<?php

namespace StudyBuddy;

/**
 * Class for parsing the SubmittedProfile
 * which is an object representing data of
 * user profile that was sent to the server from
 * the edit profile interface. Usually this is
 * sent via Edit Profile form on the web
 * but can also be submitted by other means
 * in the future like via API
 *
 *
 */
class ProfileParser extends StudyBuddyObject {

    /**
     * User object of user whose profile being updated
     *
     * @var object of type User
     */
    protected $oUser;

    /**
     * @var object of type SubmittedProfile
     */
    protected $oSubmitted;

    public function __construct(Registry $oRegistry) {
        $this->oRegistry = $oRegistry;
    }

    /**
     *
     * Modify values in User object
     * based on SubmittedProfile
     *
     * @param User $oUser
     * @param SubmittedProfile $o
     *
     * @return bool true
     */
    public function save(User $oUser, SubmittedProfile $o) {
        $this->oUser = $oUser;
        $this->oSubmitted = $o;

        $oUser['tmp_role'] = $this->getClean($o->getTmpRole())->substr(0, 60)->valueOf();
//		$oUser['fn'] 	= $this->getClean($o->getFirstName())->substr(0, 60)->valueOf();
//		$oUser['mn'] 	= $this->getClean($o->getMiddleName())->substr(0, 60)->valueOf();
//		$oUser['ln'] 	= $this->getClean($o->getLastName())->substr(0, 80)->valueOf();
        $oUser['fullname'] = $this->getClean($o->getFullName())->substr(0, 80)->valueOf();
        $oUser['sowa'] = $this->getClean($o->getSoWA())->substr(0, 80)->valueOf();
        $oUser['cc'] = $this->getClean($o->getCountry())->valueOf();
//		$oUser['state'] = $this->getClean($o->getState())->substr(0, 50)->valueOf();
//		$oUser['city'] 	= $this->getClean($o->getCity())->substr(0, 80)->valueOf();
//		$oUser['url'] 	= $this->getUrl($this->getClean($o->getUrl()));
//		$oUser['zip'] 	= $this->getClean($o->getZip())->substr(0, 8)->valueOf();
        $oUser['dob'] = $this->getDob($o->getDob());
        $oUser['gender'] = $this->getGender($o->getGender());
//		$oUser['description'] = \wordwrap($this->getClean($o->getDescription())->substr(0, 2000)->valueOf(), 50);

        if ($this->oRegistry->Viewer->offsetGet('role') == 'student' || $this->oRegistry->Viewer->offsetGet('role') == 'parent' || $this->oRegistry->Viewer->offsetGet('role') == 'registered') {
            $this->oRegistry->Viewer->setRoleId($oUser['tmp_role']);
        }

        $this->makeAvatar();
        $oUser->save();

        return true;
    }

    /**
     * Validates Dob string and returns it only if
     * it looks valid, otherwise returns null
     * @param string $string
     * @return mixed string|null null if input does not
     * look like a valid date of birth
     */
    protected function getDob($string) {
        return (Validate::validateDob($string)) ? $string : null;
    }

    /**
     * Returns value of "Gender" but only
     * if it's valid, otherwise returns null
     * Enter description here ...
     * @param string $str
     * @return string M or F or empty string
     */
    protected function getGender($str) {
        d('gender string: ' . $str);

        return ('M' === $str || 'F' === $str) ? $str : null;
    }

    /**
     * Get value of url
     * append 'http://' if url does not appear
     * to be starting with the http prefix
     * @param Utf8String $str
     * @return string
     */
    protected function getUrl(Utf8String $str) {
        $str = $str->trim();
        if ($str->length() < 4) {
            return '';
        }

        $str = $str->substr(0, 250)->trim()->valueOf();
        if ('http' !== \substr($str, 0, 4)) {
            return 'http://' . $str;
        }

        return $str;
    }

    /**
     * Get clean UTF8String object representing
     * trimmed and clean of html tags
     *
     * @param string $string
     * @return object of type UTF8String
     */
    protected function getClean($string) {
        if (empty($string)) {
            return Utf8String::factory('', 'ascii', true);
        }

        return Utf8String::factory($string)->trim()->stripTags();
    }

    /**
     * This will create avatar of square size
     * and add path to User['avatar'] IF
     * avatar has been uploaded
     *
     * @return object $this
     */
    protected function makeAvatar() {
        AvatarParser::factory($this->oRegistry)->addAvatar($this->oUser, $this->oSubmitted->getUploadedAvatar());

        return $this;
    }

}
